/**
 * 
 */
package org.crystal.jqweb.db;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.crystal.jqweb.util.BeanUtils;
/**
 * @author Crystal
 *
 */
public class ORMapping {

	private final Log log = LogFactory.getLog(getClass());
	
	/**
	 * 
	 */
	public ORMapping() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	public static Object table2Bean(Object entity,Map<String,String> tableValues) {
		Field [] fields = entity.getClass().getDeclaredFields();
		
		 for (Field field : fields) {
           	if (field.isAnnotationPresent(Column.class)) {
                Column columnAnnotation = field.getAnnotation(Column.class);
	            if(tableValues.get(columnAnnotation.name())!=null&&!tableValues.get(columnAnnotation.name()).equals("null")){
            		try {
            			String fieldType=field.getType().getName();
            			String fieldName=field.getName();
            			Object value=null;
            			LogFactory.getLog(ORMapping.class).debug("table2Bean() "+fieldName+":"+fieldType);
                        String fillValue=tableValues.get(columnAnnotation.name()).toString();
                        if(fieldType.equals("java.lang.String")){
                        	value=String.valueOf(fillValue);
                        }else if(fieldType.equals("int")){
                        	value=Integer.parseInt(fillValue);
                        }else if(fieldType.equals("long")){
                        	long longValue=Long.parseLong(fillValue);
                        	BeanUtils.set(entity, field.getName(), longValue);
                        	continue;
                        	//value=(long)fillValue);
                        }else if(fieldType.equals("java.lang.Long")){
                        	value=new Long(fillValue);//Long.parseLong(fillValue);
                        }else if(fieldType.equals("double")){
                        	value=Double.valueOf(fillValue);
                        }else if(fieldType.equals("float")){
                        	value=Float.parseFloat(fillValue);
                        }else if(fieldType.equals("java.util.Date")){ 
                        	try {
                        		if(fillValue.indexOf("/")==-1){
                        			value=(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(fillValue);
                        		}else{
                        			value=(new SimpleDateFormat("MM/dd/yyyy HH:mm:ss")).parse(fillValue);
                        		}
            				} catch (ParseException e) {
            					// TODO Auto-generated catch block
            					e.printStackTrace();
            				}
                        }else if(fieldType.equals("java.lang.Object")){
                        	value=tableValues.get(columnAnnotation);
                        }else if(fieldType.equals("char")){
                        	value=Character.valueOf(fillValue.charAt(0));
                        }else if(fieldType.equals("boolean")){
                        	value=Boolean.parseBoolean(fillValue);
                        }else if(fieldType.equals("short")){
                        	value=Short.parseShort(fillValue);
                        }else if(fieldType.equals("byte")){
                        	value=Byte.parseByte(fillValue);
                        }
                        
                        LogFactory.getLog(ORMapping.class).debug("table2Bean() field:"+field.getName()+",Type:"+field.getType()+",value:"+value);
                        BeanUtils.set(entity, field.getName(), value);
						//field.set(entity, value);
						
					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	            }
           	}
	      }
		 
		return entity;
	}

}
